package com.yuba.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuba.common.core.constant.ActionTypes;
import com.yuba.common.core.domain.model.MessageMeta;
import com.yuba.common.core.utils.MapstructUtils;
import com.yuba.common.core.utils.SpringUtils;
import com.yuba.common.mybatis.core.page.PageQuery;
import com.yuba.common.mybatis.core.page.TableDataInfo;
import com.yuba.front.domain.CollectFishSite;
import com.yuba.front.domain.bo.CollectFishSiteBo;
import com.yuba.front.domain.param.ActionParam;
import com.yuba.front.domain.vo.CollectFishSiteVo;
import com.yuba.front.event.MessageHandleEvent;
import com.yuba.front.mapper.CollectFishSiteMapper;
import com.yuba.front.service.ICollectFishSiteService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 钓场收藏管理Service业务层处理
 *
 * @author modakai
 * @date 2024-12-12
 */
@RequiredArgsConstructor
@Service
public class CollectFishSiteServiceImpl implements ICollectFishSiteService {

    private final CollectFishSiteMapper baseMapper;

    /**
     * 查询钓场收藏管理
     *
     * @param fishSiteId 主键
     * @return 钓场收藏管理
     */
    @Override
    public CollectFishSiteVo queryById(Long fishSiteId) {
        return baseMapper.selectVoById(fishSiteId);
    }

    /**
     * 分页查询钓场收藏管理列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 钓场收藏管理分页列表
     */
    @Override
    public TableDataInfo<CollectFishSiteVo> queryPageList(CollectFishSiteBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CollectFishSite> lqw = buildQueryWrapper(bo);
        Page<CollectFishSiteVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的钓场收藏管理列表
     *
     * @param bo 查询条件
     * @return 钓场收藏管理列表
     */
    @Override
    public List<CollectFishSiteVo> queryList(CollectFishSiteBo bo) {
        LambdaQueryWrapper<CollectFishSite> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CollectFishSite> buildQueryWrapper(CollectFishSiteBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CollectFishSite> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, CollectFishSite::getUserId, bo.getUserId());
        return lqw;
    }

    /**
     * 新增钓场收藏管理
     *
     * @param bo 钓场收藏管理
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(CollectFishSiteBo bo) {
        CollectFishSite add = MapstructUtils.convert(bo, CollectFishSite.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFishSiteId(add.getFishSiteId());
        }
        return flag;
    }

    /**
     * 修改钓场收藏管理
     *
     * @param bo 钓场收藏管理
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(CollectFishSiteBo bo) {
        CollectFishSite update = MapstructUtils.convert(bo, CollectFishSite.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CollectFishSite entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除钓场收藏管理信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public void collectFishSite(ActionParam body, Long userId) {
        // 1. 判断类型
        Integer fishSiteId = body.getFishSiteId();

        String type = body.getType();
        CollectFishSite data = new CollectFishSite();
        if (ActionTypes.COLLECT.equals(type)) {
            // 收藏
            data.setFishSiteId(fishSiteId);
            data.setUserId(userId);
            baseMapper.insert(data);

            // 发送收藏钓场消息
            MessageHandleEvent messageEvent = MessageHandleEvent.builder()
                .type(ActionTypes.COLLECT)
                .userId(body.getAuthorId())
                .senderId(userId)
                .sourceId(Long.valueOf(fishSiteId))
                .message("收藏了您的钓点")
                .meta(new MessageMeta()
                    .setType(ActionTypes.FISH_SITE)
                    .setFishSiteId(fishSiteId)
                )
                .build();
            SpringUtils.context().publishEvent(messageEvent);

        } else {
            // 取消收藏
            baseMapper.delete(Wrappers.<CollectFishSite>lambdaQuery()
                .eq(CollectFishSite::getFishSiteId, fishSiteId)
                .eq(CollectFishSite::getUserId, userId));
        }

    }
}
